﻿using NaturalNote.Core.Database;
using NaturalNote.Core.Mapper;
using NaturalNote.Web.DbStorage.Entity;
using NaturalNote.Web.DbStorage.Models;

namespace NaturalNote.Web.DbStorage
{
    public class PageQueryService
    {
        IDataQuery dataQuery;

        public PageQueryService(IDataQuery dataQuery)
        {
            this.dataQuery = dataQuery;
        }

        public async Task<PageViewModel> Get(long pageId)
        {
            var sql = "select * from mypages where Id=@pageId limit 1;";
            var dt = await dataQuery.Get<Pages>(sql, new { pageId });
            var rt = SMapper.Map<Pages, PageViewModel>(dt);
            return rt;
        }
        public async Task<List<PageBlockViewModel>> GetBlocks(long pageId)
        {
            var sql = "select * from mypage_blocks where PageId=@pageId;";
            var dt = await dataQuery.Query<PageBlockViewModel>(sql, new { pageId });
            var sorts = from q in dt
                        orderby q.SortNo
                        select q;
            return sorts.ToList();
        }
    }
}
